﻿(function() {
	var upUpload = window.upUpload || {};
	window.upUpload = upUpload;
	upUpload.create = function(serverurl, picker, btn, mylayer) {
		var _myuploader = {};
		if (!mylayer) {
			mylayer = layer;
		}
		_myuploader.startUpload = function() {
			
		};
		_myuploader.uploadAccept = function(obj, ret) {
			
		};
		_myuploader.uploadFinished = function() {
			
		};
		var uploader = WebUploader.create({
			swf : '/js/base/Uploader.swf',
			server : serverurl,
			pick : "#" + picker,
			resize : false
		});
		var layerIndex = -1;
		
		uploader.on('startUpload', function() {
			layerIndex = mylayer.loading();
			_myuploader.startUpload();
		});
		
		uploader.on('uploadAccept', function(obj, ret) {
			if (ret.state === 'SUCCESS') {
				_myuploader.uploadAccept(obj, ret);
			}
		});
		
		uploader.on('uploadFinished', function() {
			mylayer.alert({
				content : '上传完成'
			});
			mylayer.close(layerIndex);
			uploader.reset();
			_myuploader.uploadFinished();
		});
		uploader.on('uploadError', function(file, reason) {
			var failMsg = "上传失败，文件名：" + file.name;
			mylayer.alert({
				content : failMsg
			});
			mylayer.close(layerIndex);
			uploader.reset();
			_myuploader.uploadFinished();
		});
		
		var $updateBtn = $("#" + btn);
		$updateBtn.bind('click', function() {
			var files = uploader.getFiles();
			if (files.length <= 0) {
				mylayer.alert({
					content : '请选择要上传的文件'
				});
				return false;
			}
			uploader.upload();
		});
		return _myuploader;
	}

	upUpload.createWithPreview = function(serverurl, picker, btn, preview, previewW, previewH, mylayer) {
		var _myuploader = {};
		if (!mylayer) {
			mylayer = layer;
		}
		_myuploader.startUpload = function() {
			
		};
		_myuploader.uploadAccept = function(obj, ret) {
			
		};
		_myuploader.uploadFinished = function() {
			
		};
		var uploader = WebUploader.create({
			swf : '/js/base/Uploader.swf',
			server : serverurl,
			pick : "#" + picker,
			auto : true,
			resize : false
		});
		var layerIndex = -1;
		
		uploader.on('fileQueued', function(file) {
			if (preview) {
				var $li = $("#" + preview);
				
				uploader.makeThumb(file, function(error, ret) {
					if (error) {
						$li.text('预览错误');
					} else {
						$li.html('<img alt="" src="' + ret + '" width="' + previewW + '" height="' + previewH + '" />');
					}
				});
			}
		});
		
		uploader.on('startUpload', function() {
			layerIndex = mylayer.loading();
			_myuploader.startUpload();
		});
		
		uploader.on('uploadAccept', function(obj, ret) {
			if (ret.state === 'SUCCESS') {
				_myuploader.uploadAccept(obj, ret);
			}
		});
		
		uploader.on('uploadFinished', function() {
			mylayer.alert({
				content : '上传完成'
			});
			mylayer.close(layerIndex);
			uploader.reset();
			_myuploader.uploadFinished();
		});
		uploader.on('uploadError', function(file, reason) {
			var failMsg = "上传失败，文件名：" + file.name;
			mylayer.alert({
				content : failMsg
			});
			mylayer.close(layerIndex);
			uploader.reset();
			_myuploader.uploadFinished();
		});
		
		var $updateBtn = $("#" + btn);
		$updateBtn.bind('click', function() {
			var files = uploader.getFiles();
			if (files.length <= 0) {
				mylayer.alert({
					content : '请选择要上传的文件'
				});
				return false;
			}
			uploader.upload();
		});
		return _myuploader;
	}

	upUpload.createImgUpload = function(config, btn, preview, previewW, previewH, mylayer) {
		var _myuploader = {};
		if (!mylayer) {
			mylayer = layer;
		}
		_myuploader.startUpload = function() {
			
		};
		_myuploader.uploadAccept = function(obj, ret) {
			
		};
		_myuploader.uploadFinished = function() {
			
		};
		var uploader = WebUploader.create(config);
		
		uploader.onError = function(code) {
			if (code == 'Q_TYPE_DENIED') {
				layer.msg('图片类型错误', {
					icon : 2
				});
			} else if (code == 'F_EXCEED_SIZE') {
				layer.msg('图片的大小超过限制', {
					icon : 2
				});
			}
		};
		
		var layerIndex = -1;
		
		uploader.on('fileQueued', function(file) {
			if (preview) {
				var $li = $("#" + preview);
				
				uploader.makeThumb(file, function(error, ret) {
					if (error) {
						$li.text('预览错误');
					} else {
						$li.html('<img alt="" src="' + ret + '" width="' + previewW + '" height="' + previewH + '" />');
					}
				});
			}
		});
		
		uploader.on('startUpload', function() {
			layerIndex = mylayer.loading();
			_myuploader.startUpload();
		});
		
		uploader.on('uploadAccept', function(obj, ret) {
			if (ret.state === 'SUCCESS') {
				_myuploader.uploadAccept(obj, ret);
			}
		});
		
		uploader.on('uploadFinished', function() {
			mylayer.close(layerIndex);
			uploader.reset();
			_myuploader.uploadFinished();
		});
		uploader.on('uploadError', function(file, reason) {
			var failMsg = "上传失败，文件名：" + file.name;
			mylayer.alert({
				content : failMsg
			});
			mylayer.close(layerIndex);
			uploader.reset();
			_myuploader.uploadFinished();
		});
		
		var $updateBtn = $("#" + btn);
		$updateBtn.bind('click', function() {
			var files = uploader.getFiles();
			if (files.length <= 0) {
				mylayer.alert({
					content : '请选择要上传的文件'
				});
				return false;
			}
			uploader.upload();
		});
		return _myuploader;
	}
})();
